import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";

Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;

VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject);
  }
  return originalPush.call(this, location).catch((err) => err);
};

VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalReplace.call(this, location, onResolve, onReject);
  }
  return originalReplace.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "/home",
    name: "home",
    component: HomeView,
  },
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/List",
    name: "list",
    component: () => import("../views/List.vue"),
  },
  {
    path: "/cart",
    name: "Cart",
    component: () => import("../views/Cart.vue"),
  },
  {
    path: "/my",
    name: "my",
    component: () => import("../views/My.vue"),
  },
  {
    path: "/path",
    name: "path",
    component: () => import("../views/Path.vue"),
    children: [
      {
        path: "/",
        name: "path-index",
        component: () => import("../views/path/Path-Index.vue"),
      },
      {
        path: "path-list",
        name: "path-list",
        component: () => import("../views/path/Path-List.vue"),
      },
    ],
  },
  {
    path: "/search",
    name: "search",
    component: () => import("../views/Search.vue"),
    children: [
      {
        path: "/",
        name: "index",
        component: () => import("../views/search/Search-index.vue"),
      },
      {
        path: "search-list",
        name: "search-list",
        component: () => import("../views/search/Search-List.vue"),
      },
    ],
  },
  {
    path: "/detail",
    name: "detail",
    meta: {
      keepAlive: true, //需要缓存
    },
    component: () => import("../views/Detail.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/Login.vue"),
  },
  {
    path: "/userLogin",
    name: "UserLogin",
    component: () => import("../views/login/UserLogin.vue"),
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("../views/login/Register.vue"),
  },
  {
    path: "/recovery",
    name: "Recovery",
    component: () => import("../views/Recovery/Recovery.vue"),
    children: [
      {
        path: "/",
        name: "Index",
        component: () => import("../views/Recovery/RecoveryIndex.vue"),
      },
      {
        path: "btn",
        name: "Btn",
        component: () => import("../views/Recovery/RecoveryBtn.vue"),
      },
    ],
  },
  {
    path: "/order",
    name: "Order",
    meta: {
      keepAlive: true, //需要缓存
    },
    component: () => import("../views/Order.vue"),
  },
  {
    path: "/payment",
    name: "Payment",
    component: () => import("../views/Payment.vue"),
  },
];

const router = new VueRouter({
  // mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  let nextRoute = [
    "Payment",
    "Cart",
    "Order",
    "path",
    "path-index",
    "path-list",
  ];
  //是否登录中
  let userInfo = JSON.parse(localStorage.getItem("UserInfo"));
  //当前进入的页面，是不是需要验证的页面
  if (nextRoute.indexOf(to.name) >= 0) {
    if (!userInfo) {
      router.push("/login");
    }
  }

  next();
});

export default router;
